package com.haian.slidealbum.model.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.haian.slidealbum.R;
import com.haian.slidealbum.common.UtilsCommon;
import com.haian.slidealbum.model.object.ObjectFlickrAlbum;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class AdapterFlickrAlbum extends  ArrayAdapter<ObjectFlickrAlbum> {

	private Activity mActivity;
	private ArrayList<ObjectFlickrAlbum> listAlbum;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	@SuppressWarnings("deprecation")
	public AdapterFlickrAlbum(Activity _mActivity, ArrayList<ObjectFlickrAlbum> _listAlbum) {
		super(_mActivity, R.layout.item_folder, _listAlbum);
		this.mActivity = _mActivity;
		this.listAlbum = _listAlbum;

		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				mActivity).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.img_default)
				.showImageForEmptyUri(R.drawable.img_default)
				.showImageOnFail(R.drawable.img_default).cacheInMemory()
				.cacheOnDisc().resetViewBeforeLoading(true) // default
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
				.bitmapConfig(Bitmap.Config.RGB_565) // default
				.displayer(new FadeInBitmapDisplayer(300)) // default
				.handler(new Handler()) // default
				.build();
	}

	@Override
	public int getCount() {
		return listAlbum.size();
	}

	@Override
	public ObjectFlickrAlbum getItem(int position) {
		return listAlbum.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final Holder holder;
		if (view == null) {
			LayoutInflater inflater = mActivity.getLayoutInflater();
			view = inflater.inflate(R.layout.item_folder, parent, false);

			holder = new Holder();
			holder.itemName = (TextView) view
					.findViewById(R.id.itemName);
			holder.itemFolder = (ImageView)view.findViewById(R.id.itemFolder);
			Point size = UtilsCommon.getSizeScreen(mActivity);
			holder.itemFolder.getLayoutParams().height = size.x/3;
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}
		
		final ObjectFlickrAlbum mObjectFacebookAlbum = (ObjectFlickrAlbum) getItem(position);
		holder.itemName.setText(mObjectFacebookAlbum.getAlbum_name());
		
//		ArrayList<ObjectFacebookImage> listImg = new ArrayList<ObjectFacebookImage>();
//		listImg = mObjectFacebookAlbum.getListImage();
//		if(listImg.size() > 0)
//		{
//			imageLoader.displayImage(listImg.get(0).getImage_source(), holder.itemFolder, options);
//		}
		return view;
	}

	class Holder {
		TextView itemName;
		ImageView itemFolder;
	}

}
